#include "BerkeliumWrapper.h"

namespace BerkeliumDotNet
{
	void Berkelium::Init()
	{
		::Berkelium::init();
	}

	void Berkelium::Destroy()
	{
		::Berkelium::destroy();
	}

	void Berkelium::Update()
	{
		::Berkelium::update();
	}

	System::Drawing::Bitmap^ Berkelium::RenderToBitmap(int width, int height)
	{
		System::Drawing::Bitmap^ bitmap = gcnew System::Drawing::Bitmap(
			width, height, System::Drawing::Imaging::PixelFormat::Format32bppArgb
		);

		System::Drawing::Imaging::BitmapData^ data = bitmap->LockBits(
			System::Drawing::Rectangle(0,0, width, height),
			ImageLockMode::WriteOnly,
			bitmap->PixelFormat
		);

		::Berkelium::renderToBuffer((char*)data->Scan0.ToPointer(), width, height);
		bitmap->UnlockBits(data);

		return bitmap;
	}

	System::Drawing::Bitmap^ Berkelium::RenderToBitmap(IntPtr bitmapIn, int width, int height)
	{
		System::Drawing::Bitmap^ bitmap = gcnew System::Drawing::Bitmap(
			width, height, System::Drawing::Imaging::PixelFormat::Format32bppArgb
		);

		System::Drawing::Imaging::BitmapData^ data = bitmap->LockBits(
			System::Drawing::Rectangle(0,0, width, height),
			ImageLockMode::WriteOnly,
			bitmap->PixelFormat
		);

		memcpy(data->Scan0.ToPointer(), bitmapIn.ToPointer(), width*height*4);
		bitmap->UnlockBits(data);

		return bitmap;
	}

	void Berkelium::RenderToBitmap(Bitmap^ bitmap, IntPtr bitmapIn, Rectangle bitmapRect)
	{
		if (bitmap == nullptr)
			return;

		if (bitmapRect.Width > bitmap->Width || bitmapRect.Height > bitmap->Height)
			return;

		System::Drawing::Imaging::BitmapData^ data = bitmap->LockBits(
			bitmapRect,
			ImageLockMode::WriteOnly,
			bitmap->PixelFormat
		);

		memcpy(data->Scan0.ToPointer(), bitmapIn.ToPointer(),
			bitmapRect.Width*bitmapRect.Height*4);
		bitmap->UnlockBits(data);
	}
}
